package com.zpark.aspectj;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 切面类（通知类）
 */
@Aspect
@Component
public class Logging {

    /**
     * 定义切入点@Pointcut
     *      要求：方法必须是private，返回值类型为void，名称自定义，没有参数
     */
    @Pointcut("execution(* com.zpark.*.*.*(..))")
    private void myPointcut(){
        System.out.println("切入点方法");
    }

    /**
     * 前置通知
     */
    @Before("myPointcut()")
    public void beforeAdvice() {
        System.out.println("前置通知");
    }

    /**
     * 后置通知  相当于finally
     */
    @After("myPointcut()")
    public void afterAdvice() {
        System.out.println("后置通知");
    }

    /**
     * 返回后通知
     */
    @AfterReturning(pointcut = "myPointcut()", returning = "retVal")
    public void afterReturningAdvice(Object retVal) {
        System.out.println("返回值为：" + retVal);
    }

    /**
     * 抛出异常通知
     */
    @AfterThrowing(pointcut = "myPointcut()", throwing = "ex")
    public void afterThrowingAdvice(IllegalArgumentException ex) {
        System.out.println("这里的异常为：" + ex.toString());
    }

}
